import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';

import 'image_head.dart' as img1;
import 'simple_grid.dart' as sl1;
import 'simple_official_page.dart' as sop1;
import 'toast.dart' as toast1;
import 'http_test.dart' as http1;
import 'bottom_bar.dart' as bar1;
import 'bottom_bar2.dart' as bar2;
import 'package:v10/page_animtor.dart' as pa1;
import 'package:v10/back_drop.dart' as bd1;
import 'package:v10/keep_alive.dart' as ka1;
import 'package:v10/animation_fade.dart' as af;
import 'package:v10/animation_1.dart' as a1;
import 'package:v10/animation_2.dart' as a2;
import 'package:v10/canvas_simple.dart' as sc1;
import 'package:v10/simple_thread.dart' as st12;
import 'package:v10/simeple_lifestyle.dart' as sls1;
import 'package:v10/relative_layout.dart' as rlayout1;
import 'package:v10/simple_gesture.dart' as sg1;
import 'package:v10/appbar.dart' as appbar1;
import 'package:v10/list_slide_remove.dart' as sc2;
import 'package:v10/simple_well.dart' as sw1;
import 'package:v10/simple_popu1.dart' as sp1;
import 'package:v10/simple_popup2.dart' as sp2;
import 'package:v10/simple_popup.dart' as sp3;

main() {
//  debugPaintSizeEnabled = true;
//  debugPaintBaselinesEnabled=true;
//  debugRepaintRainbowEnabled=true;
//  timeDilation=20;
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyListView(),
    );
  }
}

class MyListView extends StatelessWidget {
  var tiles2 = [
    MyListItem(
      label: "显示图片" * 100,
      page: img1.MyApp(),
    ),
    MyListItem(
      label: "官方示例",
      page: sop1.MyApp(),
    ),
    MyListItem(
      label: "列表",
      page: sl1.MyApp(),
    ),
    MyListItem(
      label: "toast",
      page: toast1.MyApp(),
    ),
    MyListItem(
      label: "Http",
      page: http1.MyApp(),
    ),
    MyListItem(
      label: "底部导航",
      page: bar1.MyApp(),
    ),
    MyListItem(
      label: "底部导航2",
      page: bar2.MyApp(),
    ),
    MyListItem(
      label: "页面跳转动画",
      page: pa1.MyApp(),
    ),
    MyListItem(
      label: "毛玻璃效果制作",
      page: bd1.FrostedGlassDemo(),
    ),
    MyListItem(
      label: "保持页面状态",
      page: ka1.MyApp(),
    ),
    MyListItem(
      label: "动画渐变效果",
      page: af.FadeAppTest(),
    ),
    MyListItem(label: "动画列表添加删除", page: Container(child: a1.AnimatedListSample())),
    MyListItem(
      label: "动画builder",
      page: a2.MyApp(),
    ),
    MyListItem(
      label: "Canvas绘制",
      page: sc1.DemoApp(),
    ),
    MyListItem(
      label: "线程调用",
      page: st12.SampleApp(),
    ),
    MyListItem(
      label: "监听生命周期",
      page: sls1.MyApp(),
    ),
    MyListItem(
      label: "相对布局",
      page: rlayout1.MyApp(),
    ),
    MyListItem(
      label: "手势处理",
      page: sg1.SampleApp(),
    ),
    MyListItem(
      label: "appBar",
      page: appbar1.MyApp(),
    ),
    MyListItem(
      label: "触摸水波效果",
      page: sw1.MyHomePage(),
    ),
    MyListItem(
      label: "列表滑动删除",
      page: sc2.MyApp(),
    ),
    MyListItem(
      label: "顶部导航1",
      page: sp1.TabbedAppBarSample(),
    ),
    MyListItem(
      label: "顶部导航2",
      page: Container(child: sp2.AppBarBottomSample()),
    ),
    MyListItem(
      label: "顶部导航3",
      page: Container(child: sp3.BasicAppBarSample()),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text("代码练习")),
        body: NotificationListener(
            onNotification: _onNotification,
            child: RefreshIndicator(
                onRefresh: _onRefresh,
                child: ListView(
                  controller: _scrollController,
                  children: ListTile.divideTiles(
                          context: context, tiles: tiles2, color: Colors.red)
                      .toList(),
                ))));
  }

  final TrackingScrollController _scrollController =
      new TrackingScrollController();

  /**
   * 下拉刷新
   */
  Future<Null> _onRefresh() {
    final Completer<Null> completer = new Completer<Null>();
    // 启动一下 [Timer] 在3秒后，在list里面添加一条数据，关完成这个刷新
    new Timer(Duration(seconds: 3), () {
      // 完成刷新
      completer.complete(null);
    });
    return completer.future;
  }

  /**
   * 上拉加载更多
   */
  bool _onNotification(Notification notification) {
    if (notification is ScrollUpdateNotification) {
      // print('max:${_scrollController.mostRecentlyUpdatedPosition.maxScrollExtent}  offset:${_scrollController.offset}');
      // 当没去到底部的时候，maxScrollExtent和offset会相等

      if (_scrollController.mostRecentlyUpdatedPosition.maxScrollExtent >
              _scrollController.offset &&
          _scrollController.mostRecentlyUpdatedPosition.maxScrollExtent -
                  _scrollController.offset <=
              50) {
        print("_onNotification 50");
      }
      //下滑到最底部
      if (notification.metrics.extentAfter == 0.0) {
        print("_onNotification end");
      }
      //滑动到最顶部
      if (notification.metrics.extentBefore == 0.0) {
        print("_onNotification top");
      }
    }

    return false;
  }
}

class MyListItem extends StatelessWidget {
  String label;
  StatelessWidget page;

  MyListItem({Key key, this.label, this.page}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListTile(
        title: Text(
          "$label",
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
        onTap: () => goNext(context));
  }

  void goNext(var context) async {
    final result = await Navigator.push(
        context, MaterialPageRoute(builder: (context) => page));
    if (result != null) {
      Scaffold.of(context).showSnackBar(SnackBar(content: Text('$result')));
    }
  }
}
